package webjoch.process.strategies;

import webjoch.methods.Food;

import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.methods.Camera;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.task.Task;
import com.rsbuddy.script.wrappers.Npc;

public class Combat{
	/**
	 * Kills the given enemies
	 * @param enemies the given enemies
	 */
	public static void killEnemies(Npc[] enemies){
		if (enemies.length > 0){
			Npc attackNpc = getAttackNpc(enemies);
			if (attackNpc != null){
				if (!attackNpc.isInCombat() || !Players.getLocal().isInCombat()){
					if (Calculations.distanceTo(attackNpc) > 5){
						attackNpc.getLocation().clickOnMap();
						Camera.turnTo(attackNpc);
					}else{
						attackNpc.interact("Attack");
					}
					Task.sleep(2000);
				}else{
					Food.eat();
				}
			}
		}
	}
	
	/**
	 * Get the npc to attack based on his combat level
	 * @param npcs the npcs to attack
	 * @return the highest npc
	 */
	private static Npc getAttackNpc(Npc[] npcs){
		Npc attackNpc = null;
		try{
			for (Npc enemy : npcs){
				if (enemy.isDead()) continue;
				if (attackNpc == null){
					attackNpc = enemy;
					continue;
				}
				
				if (enemy.getLevel() > attackNpc.getLevel()){
					attackNpc = enemy;
				}
			}
		}catch(NullPointerException e){
		}
		return attackNpc;
	}
}
